package com.pixiv.Entity;


import com.pixiv.tools.ProgramStaticGlobal;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

@Scope("prototype")
@Component
public class User {

    private String UserName = "";
    private String name = "none";
    private int userid = -1;
    private String Email = "no EMail";
    private String QQ = "no QQ";
    private String phone = "no phone";
    private String ID = "no id";
    private String sex = "";

    private String userType = "0";
    private GetVarVector varSave = new GetVarVector();

    @Autowired
    JdbcTemplate jdbcTemplate;

    private int Level;
    private int Explosion;

    User(){
    }

    public String getUserName() {
        if (UserName == null)
            return "";
        return UserName;
    }

    public void setUserName(String userName) {
        UserName = userName;
    }

    public int getUserID() {
        return userid;
    }

    public void setUserID(int userID) {
        userid = userID;
    }

    public String getName() {
        if (name == null)
            return "";
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEMail() {
        if (Email == null)
            return "";
        return Email;
    }

    public void setEMail(String EMail) {
        this.Email = EMail;
    }

    public String getQQ() {
        if (QQ == null)
            return "";
        return QQ;
    }

    public void setQQ(String QQ) {
        this.QQ = QQ;
    }

    public String getPhone() {
        if (phone == null)
            return "";
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getID() {
        if (ID == null)
            return "";
        return ID;
    }

    public void setID(String ID) {
        this.ID = ID;
    }

    public String getSex() {
        if (sex == null)
            return "";
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public int getLevel() {
        return Level;
    }

    public void setLevel(int level) {
        Level = level;
    }

    public int getExplosion() {
        return Explosion;
    }

    public void setExplosion(int explosion) {
        Explosion = explosion;
    }

    public GetVarVector getVar() {
        return varSave;
    }

    public void setVar(GetVarVector varSave) {
        this.varSave = varSave;
    }

    public String getUserType() {
        return userType;
    }

    public void setUserType(String userType) {
        this.userType = userType;
    }

    public boolean isManager()
    {
        if ("1".equals(getUserType()))
            return true;
        return false;
    }

    public static void outPutPictureStream(HttpServletResponse resp, String road, String notFindPath, String notFindName) {

        if (road == null)
            return;

        File file = new File(ProgramStaticGlobal.getPRealPath(), road);
        if (file == null || !file.exists())
            return;

        try {
            FileInputStream fis = new FileInputStream(file);
            resp.setContentType("application/octet-stream; charset=utf-8");
            ServletOutputStream outputStream = resp.getOutputStream();
            IOUtils.copy(fis, outputStream);

        } catch (FileNotFoundException e) {
            try {
                if (notFindPath != null && notFindName != null )
                {
                    File file2 = new File(ProgramStaticGlobal.getPRealPath() + notFindPath, notFindName);
                    FileInputStream fis = new FileInputStream(file2);
                    resp.setContentType("application/octet-stream; charset=utf-8");
                    ServletOutputStream outputStream = resp.getOutputStream();
                    IOUtils.copy(fis, outputStream);
                }

            } catch (FileNotFoundException fileNotFoundException) {
                fileNotFoundException.printStackTrace();
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }


        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
